package com.shoppingmall.controller.backstage;

import com.shoppingmall.common.ServletResponse;
import com.shoppingmall.pojo.User;
import com.shoppingmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/manager/order")
public class ManagerOrderController {

    /**
     * 存储用户的session的名称
     */
    @Value("${USER_SESSION}")
    private String userSessoin;


    @Autowired
    private OrderService orderService;


    /**
     * 管理员查询所有用户的订单
     * @param session
     * @return
     */
    @RequestMapping("/list")
    public ServletResponse managerListAllUserOrder(@RequestParam(defaultValue = "10")Integer pageSize,
                                                   @RequestParam(defaultValue = "1")Integer pageNum,
                                                   HttpSession session){
        //验证是否登录或者是否是管理员  尽量不使用魔数
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession || 1 != userFromSession.getRole()){
            return ServletResponse.createByErrorMessage("用户未登录或者权限不足");
        }
        return orderService.managerListAllUserOrder(pageSize,pageNum);
    }

    @RequestMapping("/search")
    public ServletResponse searchOrderByOrderNo(HttpSession session,Long orderNo){
        //验证是否登录或者是否是管理员  尽量不使用魔数
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession || 1 != userFromSession.getRole()){
            return ServletResponse.createByErrorMessage("用户未登录或者权限不足");
        }
        if (null == orderNo){
            return ServletResponse.createByErrorMessage("参数不合法");
        }
        return orderService.searchOrderByOrderNo(orderNo);
    }

    /**
     * 查询订单详情，包括shippingvo
     * @param session
     * @param orderNo
     * @return
     */
    @RequestMapping("/detail")
    public ServletResponse queryOrderDetailByOrderNu(HttpSession session,Long orderNo){
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession || 1 != userFromSession.getRole()){
            return ServletResponse.createByErrorMessage("用户未登录或许无权限");
        }
        if (null == orderNo){
            return ServletResponse.createByErrorMessage("参数有误！！！！");
        }
        return  orderService.queryOrderDetailByOrderNu(orderNo);
    }


    @RequestMapping("/send_goods")
    public ServletResponse sendGoods(HttpSession session,Long orderNo){
        User userFromSession = (User)session.getAttribute(userSessoin);
        if (null == userFromSession || 1 != userFromSession.getRole()){
            return ServletResponse.createByErrorMessage("用户未登录或无权限");
        }
        if (null == orderNo){
            return ServletResponse.createByErrorMessage("参数有误！！！！");
        }
        return orderService.sendGoods(orderNo);



    }












}
